package co.example.michael.bigpictureloading;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import java.io.File;

/**
 * Created by Michael on 2015/3/17.
 */
public class DialogActivity extends Activity implements View.OnClickListener {

    private Button cancelButton;
    private LinearLayout selectLayout;
    private LinearLayout.LayoutParams selectParams;

    private Button albumButton;

    private Button cameraButton;

    private final static int ALBUM_REQUEST_CODE = 1;

    private final static int CAMERA_REQUEST_CODE = 2;

    private Uri cameraPictureUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_picture);

        //得到保存拍照的图片的uri
        cameraPictureUri = Uri.parse(FinalConstant.CAMERA_PICTURE_PATH);

        cancelButton = (Button) findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(this);
        albumButton = (Button) findViewById(R.id.album_button);
        albumButton.setOnClickListener(this);

        cameraButton = (Button) findViewById(R.id.camera_button);
        cameraButton.setOnClickListener(this);

//        selectLayout = (LinearLayout)findViewById(R.id.select_layout);
//        selectParams = (LinearLayout.LayoutParams)selectLayout.getLayoutParams();

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

//        int height = selectLayout.getHeight();
//        Log.e("布局高度：height=",""+height);
//
//        selectParams.bottomMargin = - height;
//        selectLayout.setLayoutParams(selectParams);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_button:
                finish();
                break;
            case R.id.album_button:
                albumButtonListener();
                break;
            case R.id.camera_button:
                cameraButtonListener();
                break;
        }
    }



    /**
     * 相册按钮的点击事件
     */
    private void albumButtonListener() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);

        intent.setType("image/*");

        startActivityForResult(intent, ALBUM_REQUEST_CODE);
    }

    /**
     * 拍照功能
     */
    private void cameraButtonListener(){

        //创建照片存储的路径
        File file = null;
        if (existSdcard()){
            file = new File(FinalConstant.CAMERA_PICTURE_PATH_2);
            if (file.exists()){
                file.delete();
            }

            if (!file.exists()){
                File vFile = file.getParentFile();
                vFile.mkdirs();
            }

        }

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT,cameraPictureUri);

        startActivityForResult(intent,CAMERA_REQUEST_CODE);
    }

    /**
     * 判断sd卡是否存在
     * @return
     */
    private boolean existSdcard(){
        if (android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            return true;
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case ALBUM_REQUEST_CODE:
                    setResult(FinalConstant.ALBUM_RESULT_CODE, data);
                    finish();
                    break;
                case CAMERA_REQUEST_CODE:
                    Log.e("到这儿了吗？？","嗯嗯嗯嗯");
                    setResult(FinalConstant.CAMERA_RESULT_CODE, data);
                    finish();
                    break;


            }
        }
    }
}
